package com.example.back.service.impl;

import com.example.back.dto.resp.EscortTypeList;
import com.example.back.mapper.EscortTypeMapper;
import com.example.back.service.EscortTypeService;
import com.example.common.entity.EscortType;
import com.example.common.support.ApiException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName EscortTypeServiceImol
 * @Author wengjt
 * @Date 2021/5/7 16:12
 * @Description
 **/
@Service
public class EscortTypeServiceImpl implements EscortTypeService {
    @Resource
    private EscortTypeMapper escortTypeMapper;

    @Override
    public EscortTypeList getEscortTypeList(Long etId, Integer showAble, Integer pageNum, Integer pageSize) {
        EscortTypeList result = new EscortTypeList();
        PageInfo<EscortType> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            escortTypeMapper.multipleSearch(etId, showAble);
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    @Override
    public EscortType getEscortType(Long etId) {
        EscortType oneById = escortTypeMapper.findOneById(etId);
        return oneById;
    }

    @Override
    public Object updateByEtId(EscortType escortType) {
        EscortType oneById = escortTypeMapper.findOneById(escortType.getEtId());
        if (oneById.getDisable() == 1) {
            throw new ApiException(400, "已删除类型不允许修改");
        }
        oneById.setName(escortType.getName());
        oneById.setShowAble(escortType.getShowAble());
        oneById.setRemark(escortType.getRemark());
        escortTypeMapper.save(oneById);
        return null;
    }

    @Override
    public Object insertOne(EscortType escortType) {
        escortType.setCreateTime(new Date());
        escortType.setDisable(0);
        escortTypeMapper.insertOne(escortType);
        return null;
    }

    @Override
    public Object deleteOne(Long etId) {
        EscortType oneById = escortTypeMapper.findOneById(etId);
        if (oneById.getShowAble() == 1) {
            throw new ApiException(400, "展示中的类型不允许删除");
        }
        escortTypeMapper.deleteOne(etId);
        return null;
    }
}
